package ui.function;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.JLabel;

import jxl.write.WriteException;


import parameter.EntryExitType;

import foundation.CCDB;
import foundation.Excel;
import foundation.Table;
import foundation.Ticket;

import business.ticket.*;

public class MainFunc {

    public void CurrentFlux(JLabel lEntry, JLabel lExit) {
        Connection conn = CCDB.getConnection();
        long lNum = 0;
        TTableFunc tableFunc = new TTableFunc();
        
        lNum = tableFunc.CurrnetFlux(conn, EntryExitType.Entry);
        lEntry.setText(String.valueOf(lNum));
        
        lNum = tableFunc.CurrnetFlux(conn, EntryExitType.Exit);
        lExit.setText(String.valueOf(lNum));
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }
    
    public void ExportDailyReport(Table flux, Table trade) throws WriteException {
        Excel excel = new Excel();
        excel.MakeFile(flux, trade);
    }
    
    public void dailyReporFill(Table tableFlux, Table tableTrade, String dateString) {
        dailyReportFluxFill(tableFlux, tableTrade, dateString);
        dailyReportTradeFill(tableTrade, dateString);
    }
    
    public void dailyReportTradeFill(Table tableTrade, String dateString) {
        Connection conn = CCDB.getConnection();
        
        ArrayList<Double> tableString = new ArrayList<Double>() ;

        String dateStart = "20110916";
        String dateYesterday = "";
        Vector<Ticket> ticketVector = new Vector<Ticket>();
        
        // Get yesterday date
        Calendar c = Calendar.getInstance(); 
        Date date=null; 
        try { 
        date = new SimpleDateFormat("yyyyMMdd").parse(dateString); 
        } catch (ParseException e) { 
        e.printStackTrace(); 
        } 
        c.setTime(date); 
        int day=c.get(Calendar.DATE); 
        c.set(Calendar.DATE,day-1); 
        dateYesterday = new SimpleDateFormat("yyyyMMdd").format(c.getTime()); 
        
        // Sale
        ticketVector.removeAllElements();
        double dNum = 0;
        double dNumTotal = 0;
        
        ticketVector.add(new TSSJT());
        ticketVector.add(new TSWelfare());
        ticketVector.add(new TSET());
        ticketVector.add(new TICNormalSale());
        for (Ticket ticket : ticketVector) {
            dNum += ticket.getSale(conn, dateYesterday, dateYesterday);
            dNumTotal += ticket.getSale(conn, dateStart, dateYesterday);
            System.out.println(ticket.getSale(conn, dateStart, dateYesterday));
        }
        // Compensation fare
        ticketVector.removeAllElements();
        ticketVector.add(new TSSJT());
        ticketVector.add(new TSPreSJT());
        ticketVector.add(new TSStationWork());
        ticketVector.add(new TSSouvenirMultiride());
        ticketVector.add(new TSMultiride());
        ticketVector.add(new TICNormalSale());
        ticketVector.add(new TICNormalRent());
        ticketVector.add(new TICElder());
        ticketVector.add(new TICStudent());
        ticketVector.add(new TSET());
        ticketVector.add(new TICSpecial());
        ticketVector.add(new TICTianYi());
        ticketVector.add(new TICSouvenir());
        ticketVector.add(new TICTest());
        for (Ticket ticket : ticketVector) {
            dNum += ticket.getCompensationFare(conn, dateYesterday, dateYesterday);
            System.out.println(ticket.getCompensationFare(conn, dateStart, dateYesterday));
            dNumTotal += ticket.getCompensationFare(conn, dateStart, dateYesterday);
        }
        tableString.add(dNum / 100);
        tableString.add(dNumTotal / 100);
        
        // ICCard consume
        dNum = 0;
        dNumTotal = 0;
        Vector<IICard> iicVector = new Vector<IICard>();
        iicVector.add(new TICTest());
        iicVector.add(new TICNormalSale());
        iicVector.add(new TICNormalRent());
        iicVector.add(new TICElder());
        iicVector.add(new TICStudent());
        iicVector.add(new TICSpecial());
        iicVector.add(new TICTianYi());
        iicVector.add(new TICSouvenir());
        for (IICard icc : iicVector) {
            dNum += icc.getConsume(conn, dateYesterday, dateYesterday);
            dNumTotal += icc.getConsume(conn, dateStart, dateYesterday);
        }
        tableString.add(dNum / 100);
        tableString.add(dNumTotal / 100);
        
        // ICCard recharge
        dNum = 0;
        dNumTotal = 0;
        iicVector.removeAllElements();
        iicVector.add(new TICTest());
        iicVector.add(new TICNormalSale());
        iicVector.add(new TICNormalRent());
        iicVector.add(new TSEmployee());
        iicVector.add(new TICStudent());
        iicVector.add(new TICSpecial());
        iicVector.add(new TICTianYi());
        iicVector.add(new TICSouvenir());
        for (IICard icc : iicVector) {
            dNum += icc.getRecharge(conn, dateYesterday, dateYesterday);
            dNumTotal += icc.getRecharge(conn, dateStart, dateYesterday);
        }
        tableString.add(dNum / 100);
        tableString.add(dNumTotal / 100);
        
        for (int iColumn = 4; iColumn < tableTrade.getColumnCount(); iColumn++) {
            String retValue = null;
            DecimalFormat df = new DecimalFormat("0.00");
            retValue = df.format(tableString.get(iColumn - 4)); 
            retValue = retValue.replaceAll(",","");  
            tableTrade.setValueAt(retValue, 0, iColumn);
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Test Excel

    }
    
    public void dailyReportFluxFill(Table tableFlux, Table tableTrade, String dateString) {
        Connection conn = CCDB.getConnection();
        EntryExitType eeType = null;
        long[][][] lNum = new long[2][3][25];           // 1 is total or accumulative, 2 is entry, exit, entry and exit, 3 is different kinds of tickets
        long lResult = 0;
        int iTicketCount = 0;
        Vector<Ticket> ticketVector = new Vector<Ticket>();
        ticketVector.add(new TSPreSJT());               // 0
        ticketVector.add(new TSSJT());                  // 1
        ticketVector.add(new TSStationWork());          // 2
        ticketVector.add(new TSSouvenirMultiride());    // 3
        ticketVector.add(new TSMultiride());            // 4
        ticketVector.add(new TSEmployee());             // 5
        ticketVector.add(new TICNormalRent());          // 6
        ticketVector.add(new TICNormalSale());          // 7
        ticketVector.add(new TICElder());               // 8
        ticketVector.add(new TICStudent());             // 9
        ticketVector.add(new TICSouvenir());            // 10
        ticketVector.add(new TICSpecial());             // 11
        ticketVector.add(new TICTianYi());              // 12
        ticketVector.add(new TICTest());                // 13
        iTicketCount = ticketVector.size();
        
        for (int i = 0; i < 3; i++) {
            switch (i) {
                case 0:
                    eeType = EntryExitType.Entry;
                    break;
                case 1:
                    eeType = EntryExitType.Exit;
                    break;
                case 2:
                    eeType = EntryExitType.EntryExit;
                    break;
                default:
            }
            for (int j = 0; j < ticketVector.size(); j++) {
                lNum[0][i][j] = ticketVector.get(j).getFlux(conn, eeType, dateString, dateString);
                lNum[0][i][iTicketCount] += lNum[0][i][j];
                
                lNum[1][i][j] = ticketVector.get(j).getFlux(conn, eeType, "20110916", dateString);
                lNum[1][i][iTicketCount] += lNum[1][i][j];
            }
        }
        
        for (int iRow = 0; iRow < tableFlux.getRowCount(); iRow++) {
            for (int iColumn = 1; iColumn < tableFlux.getColumnCount(); iColumn++) {
                switch (iColumn) {
                    case 1:
                        lResult = lNum[0][iRow][0] + lNum[0][iRow][1];
                        break;
                    case 2:
                        lResult = lNum[0][iRow][2];
                        break;
                    case 3:
                        lResult = lNum[0][iRow][3];
                        break;
                    case 4:
                        lResult = lNum[0][iRow][4];
                        break;
                    case 5:
                        lResult = lNum[0][iRow][5];
                        break;
                    case 6:
                        lResult = 0;
                        for (int i = 6; i <= 13; i++) {
                            lResult += lNum[0][iRow][i];
                        }
                        break;
                    case 7:
                        lResult = lNum[0][iRow][iTicketCount];
                        break;
                    case 8:
                        lResult = lNum[1][iRow][iTicketCount];
                        break;
                    default:
                        break;
                }
                tableFlux.setValueAt(lResult, iRow, iColumn);
            }
        }
        
        for (int iColumn = 0; iColumn < 4; iColumn++) {
            switch (iColumn) {
                case 0:
                    lResult = lNum[0][0][iTicketCount] - lNum[0][0][2] - lNum[0][0][5];
                    break;
                case 1:
                    lResult = lNum[1][0][iTicketCount] - lNum[1][0][2] - lNum[1][0][5];
                    break;
                case 2:
                    lResult = lNum[0][1][iTicketCount] - lNum[0][1][2] - lNum[0][1][5];
                    break;
                case 3:
                    lResult = lNum[1][1][iTicketCount] - lNum[1][1][2] - lNum[1][1][5];;
                    break;
                default:
                    break;
            }
            tableTrade.setValueAt(lResult, 0, iColumn);
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
